package script.mainpage
{
	import laya.components.Script;
	import laya.events.Event;
	import laya.utils.Handler;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.goods.GoodsItemVo;
	
	import script.mainpage.items.CategoryBtnCell;
	import script.mainpage.items.HomeGoodsCell;
	import script.mainpage.items.HoriGoodsCell;
	
	import ui.tabview.CategoryPanelUI;
	
	public class CategoryController extends Script
	{
		private var uiSkin:CategoryPanelUI;
		private var curselectCategory:int = 0;
		private var downPosY:Number = 0;
		private var starty:Number = 0;
		private var downList:Boolean = false;

		public var param:Object;
		
		public function CategoryController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as CategoryPanelUI;
			
			uiSkin.categoryList.itemRender = CategoryBtnCell;
			
			uiSkin.categoryList.hScrollBarSkin = "";
			uiSkin.categoryList.spaceX = 20;
			uiSkin.categoryList.renderHandler = new Handler(this, updateBtnList);
			uiSkin.categoryList.selectHandler = new Handler(this,selectCategory);
			uiSkin.categoryList.selectEnable = true;
			//uiSkin.categoryList.array = [];
			
			if(param != null && param is int)
				curselectCategory = param as int;
			else
				curselectCategory = 0;
			
			var btndata:Array = [{"btnImg":"fresh/vegetable.jpg","categoryName":"蔬菜","type":0},{"btnImg":"fresh/meat.jpg","categoryName":"肉类","type":1},{"btnImg":"fresh/fruit.jpg","categoryName":"水果","type":2},
				{"btnImg":"fresh/fish.jpg","categoryName":"水产","type":3}];
			uiSkin.categoryList.array = btndata;

			uiSkin.goodslist.itemRender = HoriGoodsCell;
			
			uiSkin.goodslist.vScrollBarSkin = "";
			uiSkin.goodslist.repeatX = 1;
			uiSkin.goodslist.spaceY = 2;
			uiSkin.goodslist.renderHandler = new Handler(this, updateGoodsList);
			uiSkin.goodslist.selectHandler = new Handler(this,selectGoods);
			uiSkin.goodslist.selectEnable = true;
			uiSkin.goodslist.array = [];
			
			
			uiSkin.goodslist.on(Event.MOUSE_DOWN,this,onCheckDrag);
			uiSkin.goodslist.on(Event.CLICK,this,onCheckDrag);
			uiSkin.goodslist.on(Event.MOUSE_UP,this,onStopDrag);
			uiSkin.goodslist.on(Event.MOUSE_OUT,this,onStopDrag);
			
			starty = uiSkin.goodslist.y;
			
			Laya.timer.frameOnce(2,this,function(){
				selectCategory(curselectCategory);
			});
		}
		
		private function updateBtnList(cell:CategoryBtnCell):void
		{
			cell.setData(cell.dataSource);
		}
		
		private function selectCategory(index:int):void
		{
			for(var i:int=0;i < uiSkin.categoryList.cells.length;i++)
			{
				(uiSkin.categoryList.cells[i] as CategoryBtnCell).clickBtn(false);
			}
			(uiSkin.categoryList.cells[index] as CategoryBtnCell).clickBtn(true);
			
			curselectCategory = uiSkin.categoryList.array[index].type;
			
			refreshGoodsList();
		}
		private function onCheckDrag(e:Event):void
		{
			downList = true;
			downPosY = e.stageY;
			if(uiSkin.goodslist.scrollBar.value <= 0)
			{
				
				uiSkin.goodslist.on(Event.MOUSE_MOVE,this,onMoveGoodlist);
			}
		}
		
		private function onMoveGoodlist(e:Event):void
		{
			var distance:Number = e.stageY - downPosY;
			if(distance > 0 && distance < 100)
			{
				uiSkin.goodslist.y = starty + distance;
			}
			else if(distance >= 100)
			{
				uiSkin.goodslist.y = starty + 100;
				
			}
		}
		private function onStopDrag():void
		{
			uiSkin.goodslist.off(Event.MOUSE_MOVE,this,onMoveGoodlist);
			if(uiSkin.goodslist.y >= starty + 100)
			{
				uiSkin.goodslist.y = starty;
				refreshGoodsList();
			}
			else if(uiSkin.goodslist.y > starty )
			{
				uiSkin.goodslist.y = starty;

			}
		}
		private function updateGoodsList(cell:HomeGoodsCell):void
		{
			cell.setData(cell.dataSource);
		}
		
		private function selectGoods(index:int):void
		{
			
		}
		
		private function refreshGoodsList():void
		{
			var postdata:String = "goodsName=" + uiSkin.searchInput.text + "&goodsType=" + curselectCategory;
			
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getGoodsList,this,onGeGoodsBack,postdata,"post");
			
		}
		
		private function onGeGoodsBack(data:Object):void
		{
			if(uiSkin.destroyed)
				return;
			var result:Object = JSON.parse(data as String);
			
			if(result.code == HttpErrorCode.OK)
			{
				var goodslist:Array = [];
				for(var i:int=0;i < result.data.length;i++)
				{
					goodslist.push(new GoodsItemVo(result.data[i]));
				}
				
				uiSkin.goodslist.array = goodslist;
			}
			
		}
		
	}
}